package com.mf.morefun.ui.myclass.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.mf.morefun.MoreFunApplication;
import com.mf.morefun.R;
import com.mf.morefun.adapter.HomeWorkAdapter;
import com.mf.morefun.adapter.ListenAdapter;
import com.mf.morefun.base.BaseBusFragment;
import com.mf.morefun.bean.ClassListBean;
import com.mf.morefun.bean.DoHomeWorkBean;
import com.mf.morefun.bean.DoHomeWorkListBean;
import com.mf.morefun.bean.ListenBean;
import com.mf.morefun.bean.ResultBean;
import com.mf.morefun.constant.Configuration;
import com.mf.morefun.listener.HomeWorkListListener;
import com.mf.morefun.requestbean.RequestClassHomeWorkBean;
import com.mf.morefun.requestbean.RequestDoHomeworkBean;
import com.mf.morefun.requestbean.RequestHomeWorkBean;
import com.mf.morefun.requestbean.RequestListenBean;
import com.mf.morefun.ui.VideoActivity;
import com.mf.morefun.ui.comment.CommentActivity;
import com.mf.morefun.ui.dohomework.HomeworkListActivity;
import com.mf.morefun.ui.dohomework.LiteraryCreationActivity;
import com.mf.morefun.ui.moreread.ListenOriginalActivity;
import com.mf.morefun.ui.moreread.ListenRecordActivity;
import com.mf.morefun.ui.moreread.ListenTeacherActivity;
import com.mf.morefun.util.HaveTestUtil;
import com.mf.morefun.widget.NetErrorPop;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhcx.zhcxlibrary.utils.DateUtils;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.SPUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class HomeWorkFragment extends BaseBusFragment {
    @BindView(R.id.lv_homework)
    ListView lvHomework;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private SPUtils mSputils;
    private String userId;
    private ClassicsHeader mClassicsHeader = null;
    private int pageNo = 1;
    private int totlePage = 1;
    private List<DoHomeWorkListBean> mDoHomeWorkList = new ArrayList<>();
    private HomeWorkAdapter adapter;
    private String mType;//0班级作业， 1班级成员下面的作业， 2人员管理下面的作业
    private String classId;
    private String userType;

    public static HomeWorkFragment newInstance(String userId, String type, String classId, String userType) {
        HomeWorkFragment newFragment = new HomeWorkFragment();
        Bundle bundle = new Bundle();
        bundle.putString("USERID", userId);
        bundle.putString("type", type);
        bundle.putString("CLASSID", classId);
        bundle.putString("USERTYPE", userType);
        newFragment.setArguments(bundle);
        return newFragment;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_homework;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mSputils = new SPUtils(getActivity());
        Bundle args = getArguments();
        if (null != args) {
            userId = args.getString("USERID");
            mType = args.getString("type");
            classId = args.getString("CLASSID");
            userType = args.getString("USERTYPE");
        } else {
            userId = "";
            mType = "";
            classId = "";
            userType = "";
        }
        if (!StringUtils.isEmpty(userId)) {
            initHomeWorkList(true);
        }
        mClassicsHeader = (ClassicsHeader) refreshLayout.getRefreshHeader();
        int delda = new Random().nextInt(7 * 24 * 60 * 60 * 1000);
        mClassicsHeader.setLastUpdateTime(new Date(System.currentTimeMillis() - delda));
        mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA));
        refreshLayout.getLayout().bringChildToFront(lvHomework);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                initHomeWorkList(true);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo++;
                if (pageNo < totlePage) {
                    initHomeWorkList(false);
                } else {
                    if (null != refreshLayout) {
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                        refreshLayout.setEnableLoadMore(false);
                    }
                }
            }
        });

        adapter = new HomeWorkAdapter(getActivity(), mDoHomeWorkList, userType);
        lvHomework.setAdapter(adapter);
    }

    public void initHomeWorkList(boolean isRefresh) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.DOHOMEWORKLIST);
        if (mType.equals("0")){
            RequestClassHomeWorkBean requestDoHomeworkBean = new RequestClassHomeWorkBean();
            RequestClassHomeWorkBean.MfTpPageDataBean mfTpPageDataBean = new RequestClassHomeWorkBean.MfTpPageDataBean();
            requestDoHomeworkBean.setPageNo(pageNo);
            requestDoHomeworkBean.setPageSize(20);
            requestDoHomeworkBean.setOrderBy("tpcdr.start_date desc");
            mfTpPageDataBean.setUserId(userId);
            mfTpPageDataBean.setClassId(classId);
            mfTpPageDataBean.setMfTpRecordFlag("1");
            requestDoHomeworkBean.setMfTpPageData(mfTpPageDataBean);
            params.setBodyContent(JSON.toJSONString(requestDoHomeworkBean));
        }else {
            RequestHomeWorkBean requestDoHomeworkBean = new RequestHomeWorkBean();
            RequestHomeWorkBean.MfTpPageDataBean mfTpPageDataBean = new RequestHomeWorkBean.MfTpPageDataBean();
            requestDoHomeworkBean.setPageNo(pageNo);
            requestDoHomeworkBean.setPageSize(20);
            requestDoHomeworkBean.setOrderBy("tpcdr.start_date desc");
            mfTpPageDataBean.setUserId(userId);
            mfTpPageDataBean.setMfTpRecordFlag("1");
            requestDoHomeworkBean.setMfTpPageData(mfTpPageDataBean);
            params.setBodyContent(JSON.toJSONString(requestDoHomeworkBean));
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (null != refreshLayout) {
                    refreshLayout.finishLoadMore();
                    refreshLayout.finishRefresh();
                }
                if (isRefresh) {
                    mDoHomeWorkList.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0) {
                        if (!StringUtils.isEmpty(resultBean.getResult())) {
                            DoHomeWorkBean doHomeWorkBean = JSON.parseObject(resultBean.getResult(), DoHomeWorkBean.class);
                            totlePage = doHomeWorkBean.getTotalPage();
                            if (!StringUtils.isEmpty(doHomeWorkBean.getList())) {
                                List<DoHomeWorkListBean> doHomeWorkListBeanList = JSON.parseArray(doHomeWorkBean.getList(), DoHomeWorkListBean.class);
                                mDoHomeWorkList.addAll(doHomeWorkListBeanList);
                                //拿到播放的list
                                adapter.setListener(new HomeWorkListListener() {
                                    @Override
                                    public void callBack(int position, int type) {
                                        if (mDoHomeWorkList.size() > 0) {
                                            if (type == 0) {
                                                Intent mIntent = new Intent(getActivity(), LiteraryCreationActivity.class);
                                                mIntent.putExtra(Configuration.USERID, userId);
                                                if (null != mDoHomeWorkList.get(position).getMfTpLRR()){
                                                    mIntent.putExtra("TPBUS", mDoHomeWorkList.get(position).getMfTpLRR().getId());
                                                    mIntent.putExtra("TPDETAILID", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getId());
                                                    mIntent.putExtra("TP", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getTp().getId());
                                                }
                                                if (null != mDoHomeWorkList.get(position).getMfTpCreation()) {
                                                    mIntent.putExtra("TITLE", mDoHomeWorkList.get(position).getMfTpCreation().getName());
                                                    mIntent.putExtra("DESCRIPTION", mDoHomeWorkList.get(position).getMfTpCreation().getTextDescription());
                                                }
                                                mIntent.putExtra("CLASSID", mDoHomeWorkList.get(position).getClassId());
                                                startActivityForResult(mIntent, 0);
                                            } else if (type == 1) {
                                                Intent mIntent = new Intent(getActivity(), CommentActivity.class);
                                                mIntent.putExtra("TYPE", "1");
                                                mIntent.putExtra(Configuration.USERID, userId);
                                                mIntent.putExtra(Configuration.HOMEWORK, caluateDate(DateUtils.DATE_YEAR_MONTH_DAY.format(new Date(System.currentTimeMillis())), mDoHomeWorkList.get(position).getEndDate()));
                                                if (null != mDoHomeWorkList.get(position).getMfTpCreation()){
                                                    mIntent.putExtra("TITLE", mDoHomeWorkList.get(position).getMfTpCreation().getName());
                                                    mIntent.putExtra("DESCRIPTION", mDoHomeWorkList.get(position).getMfTpCreation().getTextDescription());
                                                }
                                                if (null != mDoHomeWorkList.get(position).getMfTpLRR()){
                                                    mIntent.putExtra("TPBUS", mDoHomeWorkList.get(position).getMfTpLRR().getId());
                                                    mIntent.putExtra("TPDETAILID", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getId());
                                                    mIntent.putExtra("TP", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getTp().getId());
                                                }
                                                if (null != mDoHomeWorkList.get(position).getMfTpRecord()) {
                                                    mIntent.putExtra("MFRECORDID", mDoHomeWorkList.get(position).getMfTpRecord().getId());
                                                    if (mDoHomeWorkList.get(position).getMfTpRecord().getType().equals("5")) {
                                                        mIntent.putExtra("ISVIDEO", mDoHomeWorkList.get(position).getMfTpRecord().getAudio());
                                                    }
                                                    if (null != mDoHomeWorkList.get(position).getMfTpRecord().getUser()) {
                                                        mIntent.putExtra("STUDENT", mDoHomeWorkList.get(position).getMfTpRecord().getUser().getId());
                                                    }
                                                    if (!StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpCommentFlag())){
                                                        if (mDoHomeWorkList.get(position).getMfTpCommentFlag().equals("1")) {//有点评才会传值过去
                                                            mIntent.putExtra("MFCOMMENT", mDoHomeWorkList.get(position).getMfTpComment());
                                                        }
                                                    }
                                                }
                                                mIntent.putExtra("CLASSID", mDoHomeWorkList.get(position).getClassId());
                                                startActivity(mIntent);
                                            } else {
                                                if (type != 2) {//创作
                                                    if (MoreFunApplication.loginBean.getResult().getUserType().equals("1")){
                                                        try {
                                                            showDetailDialog(position, type);
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        }
                                                    }else {
                                                        Intent mIntent = new Intent(getActivity(), CommentActivity.class);
                                                        mIntent.putExtra(Configuration.HOMEWORK, caluateDate(DateUtils.DATE_YEAR_MONTH_DAY.format(new Date(System.currentTimeMillis())), mDoHomeWorkList.get(position).getEndDate()));
                                                        mIntent.putExtra("BOOKID", mDoHomeWorkList.get(position).getMfTpLRR().getBook().getId());
                                                        mIntent.putExtra(Configuration.USERID, userId);
                                                        mIntent.putExtra("BOOKNAME", mDoHomeWorkList.get(position).getMfTpLRR().getBook().getName());
                                                        mIntent.putExtra("TYPE", "1");
                                                        mIntent.putExtra("CLASSID", mDoHomeWorkList.get(position).getClassId());
                                                        mIntent.putExtra("TPBUS", mDoHomeWorkList.get(position).getMfTpLRR().getId());
                                                        mIntent.putExtra("TPDETAILID", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getId());
                                                        mIntent.putExtra("TP", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getTp().getId());
                                                        if (null != mDoHomeWorkList.get(position).getMfTpRecord()) {
                                                            mIntent.putExtra("MFRECORDID", mDoHomeWorkList.get(position).getMfTpRecord().getId());
                                                            if (mDoHomeWorkList.get(position).getMfTpRecord().getType().equals("5")) {
                                                                mIntent.putExtra("ISVIDEO", mDoHomeWorkList.get(position).getMfTpRecord().getAudio());
                                                            }
                                                            if (null != mDoHomeWorkList.get(position).getMfTpRecord().getUser()) {
                                                                mIntent.putExtra("STUDENT", mDoHomeWorkList.get(position).getMfTpRecord().getUser().getId());
                                                            }
                                                        }
                                                        if (!StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpCommentFlag())){
                                                            if (mDoHomeWorkList.get(position).getMfTpCommentFlag().equals("1")){//有点评才会传值过去
                                                                mIntent.putExtra("MFCOMMENT", mDoHomeWorkList.get(position).getMfTpComment());
                                                            }
                                                        }
                                                        startActivity(mIntent);
                                                    }
                                                } else {
                                                    //进入视频
                                                    Intent mIntent = new Intent(getActivity(), VideoActivity.class);
                                                    mIntent.putExtra(Configuration.USERID, userId);
                                                    if (null != mDoHomeWorkList.get(position).getMfTpLRR()) {
                                                        mIntent.putExtra("TPBUS", mDoHomeWorkList.get(position).getMfTpLRR().getId());
                                                        mIntent.putExtra("TPDETAILID", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getId());
                                                        mIntent.putExtra("TP", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getTp().getId());
                                                        mIntent.putExtra("type", "4");
                                                        mIntent.putExtra("BOOKID", mDoHomeWorkList.get(position).getMfTpLRR().getBook().getId());
                                                    }else if (null != mDoHomeWorkList.get(position).getMfTpCreation()) {
                                                        mIntent.putExtra("TPBUS", mDoHomeWorkList.get(position).getMfTpCreation().getId());
                                                        mIntent.putExtra("TPDETAILID", mDoHomeWorkList.get(position).getMfTpCreation().getTeachingProgramDetail().getId());
                                                        mIntent.putExtra("type", "4");
                                                    }
                                                    mIntent.putExtra("VIDEO", mDoHomeWorkList.get(position).getMfVideo().getAddress());
                                                    mIntent.putExtra("CLASSID", mDoHomeWorkList.get(position).getClassId());
                                                    startActivity(mIntent);
                                                }
                                            }
                                        }
                                    }
                                });
                            }
                        }
                    } else {
                        ToastUtils.show(getActivity(), resultBean.getMessage(), 100);
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (null != refreshLayout) {
                    refreshLayout.finishLoadMore();
                    refreshLayout.finishRefresh();
                }
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //点击弹出可以
    private void showDetailDialog(int position, int type) {
        final Dialog dialog = new Dialog(getActivity(), R.style.loginerrordialog);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_dialogdetail, null);
        dialog.setContentView(view);
        LinearLayout ll_explain = view.findViewById(R.id.ll_explain);
        LinearLayout ll_fundamental = view.findViewById(R.id.ll_fundamental);
        LinearLayout ll_recording = view.findViewById(R.id.ll_recording);
        LinearLayout ll_test = view.findViewById(R.id.ll_test);
        TextView tv_recording = view.findViewById(R.id.tv_recording);
        if (type == 3) {
            tv_recording.setText("录音");
        } else {
            tv_recording.setText("看作业");
        }
        if (StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpLRR().getBook().getHaveListenFlag())) {
            ll_explain.setBackgroundResource(R.drawable.gray_shape);
        } else {
            if (mDoHomeWorkList.get(position).getMfTpLRR().getBook().getHaveListenFlag().equals("1")) {
                ll_explain.setBackgroundResource(R.drawable.blue_shape);
            } else {
                ll_explain.setBackgroundResource(R.drawable.gray_shape);
            }
        }
        if (StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpLRR().getBook().getHaveTestFlag())) {
            ll_test.setBackgroundResource(R.drawable.gray_shape);
        } else {
            if (mDoHomeWorkList.get(position).getMfTpLRR().getBook().getHaveTestFlag().equals("1")) {
                ll_test.setBackgroundResource(R.drawable.blue_shape);
            } else {
                ll_test.setBackgroundResource(R.drawable.gray_shape);
            }
        }
        initListener(position, ll_explain, ll_fundamental, ll_recording, ll_test, dialog, type);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        dialog.show();
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (DeviceUtils.deviceWidth(mContext) * 0.8);
        lp.height = (int) (DeviceUtils.deviceWidth(mContext) * 0.22);
        dialogWindow.setAttributes(lp);
    }

    private void initListener(int position, LinearLayout ll_explain, LinearLayout ll_fundamental, LinearLayout ll_recording, LinearLayout ll_test, Dialog dialog, int type) {
        ll_explain.setOnClickListener(new View.OnClickListener() {//听讲解
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpLRR().getBook().getHaveListenFlag())) {
                    ToastUtils.show(getActivity(), "当前还没有老师讲解呢！", 100);
                } else {
                    if (mDoHomeWorkList.get(position).getMfTpLRR().getBook().getHaveListenFlag().equals("1")) {
                        dialog.dismiss();
                        String listenId = "";
                        if (null != mDoHomeWorkList.get(position).getMfTpLRR().getListen()){
                            if (!StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpLRR().getListen().getId())){
                                listenId = mDoHomeWorkList.get(position).getMfTpLRR().getListen().getId();
                            }
                        }
                        //调接口
                        initGetListen(mDoHomeWorkList.get(position).getMfTpLRR().getBook().getId(), listenId);
                    } else {
                        ToastUtils.show(getActivity(), "当前还没有老师讲解呢！", 100);
                    }
                }
            }
        });
        ll_fundamental.setOnClickListener(new View.OnClickListener() {//听原音
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(getActivity(), ListenOriginalActivity.class);
                mIntent.putExtra("BOOKID", mDoHomeWorkList.get(position).getMfTpLRR().getBook().getId());
                mIntent.putExtra("BOOKNAME", mDoHomeWorkList.get(position).getMfTpLRR().getBook().getName());
                mIntent.putExtra(Configuration.USERID, userId);
                startActivity(mIntent);
            }
        });
        ll_recording.setOnClickListener(new View.OnClickListener() {//录音
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent mIntent = null;
                if (type == 3) {
                    mIntent = new Intent(getActivity(), ListenRecordActivity.class);
                    mIntent.putExtra(Configuration.USERID, userId);
                    mIntent.putExtra("BOOKID", mDoHomeWorkList.get(position).getMfTpLRR().getBook().getId());
                    mIntent.putExtra("BOOKNAME", mDoHomeWorkList.get(position).getMfTpLRR().getBook().getName());
                    mIntent.putExtra("TYPE", "2");
                    mIntent.putExtra("CLASSID", mDoHomeWorkList.get(position).getClassId());
                    mIntent.putExtra("TPBUS", mDoHomeWorkList.get(position).getMfTpLRR().getId());
                    mIntent.putExtra("TPDETAILID", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getId());
                    mIntent.putExtra("TP", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getTp().getId());
                    if (null != mDoHomeWorkList.get(position).getMfTpRecord()) {
                        mIntent.putExtra("MFRECORDID", mDoHomeWorkList.get(position).getMfTpRecord().getId());
                        mIntent.putExtra("CONTEXTID", mDoHomeWorkList.get(position).getMfTpRecord().getId());
                        if (mDoHomeWorkList.get(position).getMfTpRecord().getType().equals("5")) {
                            mIntent.putExtra("ISVIDEO", mDoHomeWorkList.get(position).getMfTpRecord().getAudio());
                        }
                        if (null != mDoHomeWorkList.get(position).getMfTpRecord().getUser()) {
                            mIntent.putExtra("STUDENT", mDoHomeWorkList.get(position).getMfTpRecord().getUser().getId());
                        }
                    }
                    if (!StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpCommentFlag())){
                        if (mDoHomeWorkList.get(position).getMfTpCommentFlag().equals("1")){//有点评才会传值过去
                            mIntent.putExtra("MFCOMMENT", mDoHomeWorkList.get(position).getMfTpComment());
                        }
                    }
                    mIntent.putExtra("CONTEXTTYPE", "2");
                    mIntent.putExtra("MEDIATYPE", "1");
                    startActivityForResult(mIntent, 1);
                } else {
                    mIntent = new Intent(getActivity(), CommentActivity.class);
                    mIntent.putExtra(Configuration.HOMEWORK, caluateDate(DateUtils.DATE_YEAR_MONTH_DAY.format(new Date(System.currentTimeMillis())), mDoHomeWorkList.get(position).getEndDate()));
                    mIntent.putExtra("BOOKID", mDoHomeWorkList.get(position).getMfTpLRR().getBook().getId());
                    mIntent.putExtra(Configuration.USERID, userId);
                    mIntent.putExtra("BOOKNAME", mDoHomeWorkList.get(position).getMfTpLRR().getBook().getName());
                    mIntent.putExtra("TYPE", "1");
                    mIntent.putExtra("CLASSID", mDoHomeWorkList.get(position).getClassId());
                    mIntent.putExtra("TPBUS", mDoHomeWorkList.get(position).getMfTpLRR().getId());
                    mIntent.putExtra("TPDETAILID", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getId());
                    mIntent.putExtra("TP", mDoHomeWorkList.get(position).getMfTpLRR().getTpDetail().getTp().getId());
                    if (null != mDoHomeWorkList.get(position).getMfTpRecord()) {
                        mIntent.putExtra("MFRECORDID", mDoHomeWorkList.get(position).getMfTpRecord().getId());
                        if (mDoHomeWorkList.get(position).getMfTpRecord().getType().equals("5")) {
                            mIntent.putExtra("ISVIDEO", mDoHomeWorkList.get(position).getMfTpRecord().getAudio());
                        }
                        if (null != mDoHomeWorkList.get(position).getMfTpRecord().getUser()) {
                            mIntent.putExtra("STUDENT", mDoHomeWorkList.get(position).getMfTpRecord().getUser().getId());
                        }
                    }
                    if (!StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpCommentFlag())){
                        if (mDoHomeWorkList.get(position).getMfTpCommentFlag().equals("1")){//有点评才会传值过去
                            mIntent.putExtra("MFCOMMENT", mDoHomeWorkList.get(position).getMfTpComment());
                        }
                    }
                    startActivity(mIntent);
                }
            }
        });
        ll_test.setOnClickListener(new View.OnClickListener() {//测试
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(mDoHomeWorkList.get(position).getMfTpLRR().getBook().getHaveTestFlag())) {
                    ToastUtils.show(getActivity(), "当前绘本还没有测试题！", 100);
                } else {
                    if (mDoHomeWorkList.get(position).getMfTpLRR().getBook().getHaveTestFlag().equals("1")) {
                        HaveTestUtil mHave = new HaveTestUtil();
                        mHave.initGetTest(getActivity(), mDoHomeWorkList.get(position).getMfTpLRR().getBook().getId(), userId);
                    } else {
                        ToastUtils.show(getActivity(), "当前绘本还没有测试题！", 100);
                    }
                }
            }
        });
    }

    private boolean caluateDate(String startDate, String endDate) {
        boolean isAble = false;
        if (StringUtils.isEmpty(startDate)) {
            return false;
        }
        if (StringUtils.isEmpty(endDate)) {
            return false;
        }
        String start[] = startDate.split("-");
        String end[] = endDate.split("-");
        if (start.length != 3) {
            return false;
        }
        if (end.length != 3) {
            return false;
        }
        try {
            int year = Integer.parseInt(end[0]) - Integer.parseInt(start[0]);
            int month = Integer.parseInt(end[1]) - Integer.parseInt(start[1]);
            int day = Integer.parseInt(end[2]) - Integer.parseInt(start[2]);
            int time = (year * 365 + month * 30 + day);
            if (time < 0) {
                isAble = false;
            } else {
                isAble = true;
            }
        } catch (Exception e) {
            isAble = false;
            e.printStackTrace();
        }
        return isAble;
    }

    private void initGetListen(String bookId, String listenId) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.LISTENTEACHER);
        RequestListenBean requestListenBean = new RequestListenBean();
        RequestListenBean.BookBean bookBean = new RequestListenBean.BookBean();
        bookBean.setId(bookId);
        requestListenBean.setBook(bookBean);
        params.setBodyContent(JSON.toJSONString(requestListenBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!StringUtils.isEmpty(result)) {
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0) {
                        if (!StringUtils.isEmpty(resultBean.getResult())) {
                            List<ListenBean> listenBeanList = JSON.parseArray(resultBean.getResult(), ListenBean.class);
                            if (null != listenBeanList && listenBeanList.size() > 0){
                                try {
                                    int selectItem = 0;
                                    if (!StringUtils.isEmpty(listenId)){
                                        for (int i = 0; i < listenBeanList.size(); i++){
                                            if (listenId.equals(listenBeanList.get(i).getId())){
                                                selectItem = i;
                                            }
                                        }
                                    }
                                    showExplainPop(listenBeanList, selectItem);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }

                            }
                        } else {
                            ToastUtils.show(getActivity(), "数据为空", 100);
                        }
                    } else {
                        ToastUtils.show(getActivity(), resultBean.getMessage(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private PopupWindow mExplainPop = null;

    private void showExplainPop(List<ListenBean> listenBeanList, int selectItem) {
        closePop();
        if (mExplainPop == null) {
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.layout_popexplain, null);
            // 这里启动头像
            RelativeLayout rl_bg = (RelativeLayout) view.findViewById(R.id.rl_bg);
            rl_bg.getBackground().setAlpha(100);
            ListView lv_explain = view.findViewById(R.id.lv_explain);
            ListenAdapter adapter = new ListenAdapter(getActivity(), listenBeanList);
            adapter.setSelectPosition(selectItem + "");
            lv_explain.setAdapter(adapter);
            lv_explain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //开始听讲解
                    Intent mIntent = new Intent(getActivity(), ListenTeacherActivity.class);
                    mIntent.putExtra("BOOKNAME", listenBeanList.get(position).getName());
                    mIntent.putExtra("LISTENBEAN", listenBeanList.get(position));
                    mIntent.putExtra(Configuration.USERID, userId);
                    startActivity(mIntent);
                    closePop();
                }
            });
            TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
            tv_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    closePop();
                }
            });
            int h = getActivity().getWindowManager().getDefaultDisplay().getHeight();
            mExplainPop = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, h);
            mExplainPop.setFocusable(true);
            mExplainPop.setOutsideTouchable(true);
            mExplainPop.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // 这里如果返回true的话，touch事件将被拦截
                    // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                    return false;
                }
            });
            mExplainPop.setBackgroundDrawable(new BitmapDrawable());
            Rect rect = new Rect();
            getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            int winHeight = getActivity().getWindow().getDecorView().getHeight();
            mExplainPop.showAtLocation(getActivity().getWindow().getDecorView(), Gravity.BOTTOM, 0, winHeight - rect.bottom);
        }
    }

    private void closePop() {
        if (null != mExplainPop) {
            mExplainPop.dismiss();
            mExplainPop = null;
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 101:
                    NetErrorPop errorPop = new NetErrorPop(getActivity());
                    errorPop.showPop("");
                    break;
            }
        }
    };

    @Override
    protected void initData() {

    }

}
